package com.juyu.miliao.api.util;


import com.juyu.miliao.api.cache.GradeCache;
import com.juyu.miliao.api.dao.redis.LineTimeRedisDao;
import com.juyu.miliao.api.dao.redis.UserCallRedisDao;
import com.juyu.miliao.api.domain.Grade;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.common.util.DoubleFormatUtil;
import com.juyu.miliao.common.util.SumAgeUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户赋值
 * @Auther: 卜加超
 * @Date: 2019/5/23 09:55
 * @Description:
 */
public class UserAssignmentUtil {

    public static void  UserAssignment(User user,LineTimeRedisDao lineTimeRedisDao,UserCallRedisDao userCallRedisDao){
//        List<Grade> mgradeList = GradeCache.INSTANCE.getGradeList(1);
//        List<Grade> vgradeList = GradeCache.INSTANCE.getGradeList(2);
        //魅力等级
//        user.setmGrade(GradeUtil.getGradeCount(mgradeList, user.getmGradeSum()));
        // 土豪等级
//        user.setvGrade(GradeUtil.getGradeCount(vgradeList, user.getvGradeSum()));
        // 生日转换成年龄
        if (user.getBirthday() != null) {
            user.setAge(SumAgeUtil.getAgeByBirth(user.getBirthday()));
            user.setBirthdayInstant(user.getBirthday().toString());
        }
        if(user.getIsHost()==1){
            //主播状态设置 0离线，1在线，2繁忙，3勿扰
            // 判断主播是否在线
            if (lineTimeRedisDao.isMember(user.getUserId() + ""))
                user.setIsLine(1);
            //如果用户为主播且正在通话中，则为繁忙状态
            if (user.getIsHost() == 1 && userCallRedisDao.findHostCall(user.getUserId() + ""))
                user.setIsLine(2);
            // 判断用户是否勿扰
            if (user.getIsVideo() == 2)
                user.setIsLine(3);
        }else {
            // 判断用户是否在线
            if (lineTimeRedisDao.hasKeyLine(user.getUserId() + ""))
                user.setIsLine(1);
            // 判断用户是否勿扰
            if (user.getIsVideo() == 2)
                user.setIsLine(3);

        }
        //获取接通率
        double callSum = user.getCallSuccessSum() + user.getCallFailSum();
        if (callSum != 0) {
            double rate = (Double.parseDouble(user.getCallSuccessSum() + "") / callSum);
            user.setCallRate(DoubleFormatUtil.intFormate(DoubleFormatUtil.doubleFormate(rate) * 100) + "%");
        } else {
            user.setCallRate("100%");
        }

    }

}
